package cn.git.future;

import java.util.concurrent.*;

/**
 * @description: 初始了解completableFuture
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-08-07
 */
public class CompletableFutureDemo01 {

    static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,
            50,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(100),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程start ......");
        // 没有返回结果，可以执行任务
        CompletableFuture.runAsync(() -> {
            System.out.println("子线程执行了：" + Thread.currentThread().getName());
        }, executor);

        // 带返回结果
        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("子线程2执行了：" + Thread.currentThread().getName());
            return 10;
        }, executor);
        Integer result = future2.get();
        System.out.println("result = " + result);

        System.out.println("主线程end ......");
    }
}
